﻿using FairyGUI;

/// <summary>
/// 单局结算
/// </summary>
public class UIGameRoundSett : UIPopupWindow {

	class PlayerSettInfo {
		UIHeadpicImage _headpic;
		GTextField _name;
		GTextField _score;
		GTextField _roundScore;
		GImage _winImg;
		GImage _loseImg;

		public PlayerSettInfo(GComponent uiComp) {
			_headpic = new UIHeadpicImage(uiComp.GetChild("头像").asCom);
			_name = uiComp.GetChild("名字").asTextField;
			_score = uiComp.GetChild("总分").asTextField;
			_roundScore = uiComp.GetChild("单局分").asTextField;
			_winImg = uiComp.GetChild("胜利").asImage;
			_loseImg = uiComp.GetChild("失败").asImage;
		}

		public void Set(GameDataKernel.RoundSett.PlayerSett info) {
			_headpic.SetHeadpic(info.headpic);
			_name.text = info.name;
			_score.text = "总分：" + info.score;
			if (info.win) {
				_roundScore.text = "[color=#ff0000]+" + info.roundScore + "[/color]";

				_winImg.visible = true;
				_loseImg.visible = false;
			} else {
				_roundScore.text = "[color=#000000]" + info.roundScore + "[/color]";

				_winImg.visible = false;
				_loseImg.visible = true;
			}
		}
	}

	PlayerSettInfo[] _infos;

	public UIGameRoundSett()
    {
        // 加载界面
        base.Init(UIPackage.CreateObject("游戏界面", "单局结算").asCom);

        // 初始化准备游戏按钮
		GButton prepareBtn = uiComp.GetChild("准备游戏按钮").asButton;
		prepareBtn.onClick.Add(() => {
            _Close();
        });

		_infos = new PlayerSettInfo[5];
		for (int i = 1; i <= 5; ++i) {
			_infos [i - 1] = new PlayerSettInfo (uiComp.GetChild("玩家" + i).asCom);
		}
			
		foreach (GameDataKernel.RoundSett.PlayerSett playerSett in GameDataKernel.inst.roundSett.setts) {
			int diff = playerSett.index - GameDataKernel.inst.playerIndex;
			if (diff < 0)
			{
				diff += 5;
			}
			_infos [diff].Set (playerSett);
		}
    }

    void _Close()
    {
        DebugInfo.Log("关闭单局结算");

		GameProcedure.inst.EnqueueMessage (
			new BaseProceduar.Message(Commands.CMD_MOD_MSG, GameCommands.CMD_PREPARE_GAME));

        AudioController.inst.PlayUIClickEffect();
        uiComp.RemoveFromParent();
        uiComp.Dispose();
        base.Close();
    }

    /// <summary>
    /// 弹出窗口
    /// </summary>
    public static void Popup()
    {
		UIGameLeaveConfirm.CloseWindow ();

        DebugInfo.Log("打开单局结算");
		UIRootController.inst.PopupWindow(new UIGameRoundSett());
    }
}
